<?php
namespace App\Services;
use GuzzleHttp\Client;
use App\Services\WechatopenService;
use App\Models\Miniappsauthorization;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class MiniappopenService
{
    public static function miniappAccessToken($componentAppid,$appId)
    {
        $accessToken = Redis::get("authorizer_access_token_".$componentAppid."_".$appId);
        if (empty($accessToken)) {
            $miniappSautHorization = Miniappsauthorization::getOpenTokenDetial($appId);
            if (empty($miniappSautHorization)) return ["status"=>"error","msg"=>"微信小程序不存在!","access_token"=>""];

            $accessToken = WechatopenService::getMiniProgramAccessToken($miniappSautHorization->component_appid,$miniappSautHorization->authorizer_appid,$miniappSautHorization->authorizer_refresh_token);
            Redis::setex("miniapp_access_token_".$appId,7100,$accessToken);
            Redis::setex("authorizer_access_token_".$componentAppid."_".$appId,7100,$accessToken);

        }
        return ["status"=>"success","msg"=>"","access_token"=>$accessToken];
    }

    // 创建试用小程序
    public static function fastRegisterBetaWeApp($component_appid,$name,$openid)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["name"=>$name,"openid"=>$openid];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/component/fastregisterbetaweapp?access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 验证试用小程序
    public static function verifyBetaWeApp($component_appid,$authorizer_appid,$authorizer_refresh_token,$enterprise_name,$code,$code_type,$legal_persona_wechat,$legal_persona_name,$legal_persona_idcard,$component_phone)
    {
        $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $verify_info = ["verify_info"=>["enterprise_name"=>$enterprise_name,"code"=>$code,"code_type"=>$code_type,"legal_persona_wechat"=>$legal_persona_wechat,"legal_persona_name"=>$legal_persona_name,"legal_persona_idcard"=>$legal_persona_idcard,"component_phone"=>$component_phone]];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/verifybetaweapp?access_token='.$authorizer_access_token, [
            'body' => json_encode($verify_info,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 使用授权码获取授权信息
    public static function apiQueryAuth($component_appid,$authorization_code)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"authorization_code"=>$authorization_code];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 第三方平台调用快速注册 API 完成注册
    public static function accountFastRegister($component_appid,$authorizer_appid,$authorizer_refresh_token,$ticket,$status)
    {
        if ($status == 1) {
            $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        } else {
            $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        }
        $client = new Client;
        $params = ["ticket"=>$ticket];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/account/fastregister?access_token='.$authorizer_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取/刷新接口调用令牌
    public static function apiAuthorizerToken($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizerAccessToken = Redis::get("authorizer_access_token_".$component_appid."_".$authorizer_appid);
        if (empty($authorizerAccessToken)) {
            $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
            $client = new Client;
            $params = ["component_appid"=>$component_appid,"authorizer_appid"=>$authorizer_appid,"authorizer_refresh_token"=>$authorizer_refresh_token];
            $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.$component_access_token, [
                'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
                'headers' => [
                    'Content-type'=> 'application/json',
                    'Accept'=>'application/json'
                ]
            ]);
            $body = $response->getBody();
            $jsonbody = json_decode($body,true);
            $authorizerAccessToken = $jsonbody["authorizer_access_token"];
            Redis::setex("authorizer_access_token_".$component_appid."_".$authorizer_appid,7100,$authorizerAccessToken);
        }
        return $authorizerAccessToken;
    }

    // 上传小程序代码并生成体验版
    public static function wxaCommit($access_token,$template_id,$ext_json,$user_version,$user_desc)
    {
        $client = new Client;
        $params = ["template_id"=>$template_id,"ext_json"=>$ext_json,"user_version"=>$user_version,"user_desc"=>$user_desc];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/commit?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取体验版二维码
    public static function wxaGetQrcode($component_appid,$authorizer_appid,$authorizer_refresh_token,$path)
    {
        $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $content = file_get_contents('https://api.weixin.qq.com/wxa/get_qrcode?access_token='.$authorizer_access_token.'&path='.urlencode($path));
        $qrcode = public_path("qrcodes/".time().rand(1000,9999).".jpg");
        file_put_contents($qrcode, $content); // 写入文件
        return $qrcode;
    }

    // 提交审核
    public static function wxaSubmitAudit($access_token,$item_list,$preview_info,$version_desc,$feedback_info,$feedback_stuff,$ugc_declare)
    {
        $client = new Client;
        // $params = ["item_list"=>$item_list,"preview_info"=>$preview_info,"version_desc"=>$version_desc,"feedback_info"=>$feedback_info,"feedback_stuff"=>$feedback_stuff,"ugc_declare"=>$ugc_declare];
        // $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/submit_audit?access_token='.$authorizer_access_token, [
        //     'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
        //     'headers' => [
        //         'Content-type'=> 'application/json',
        //         'Accept'=>'application/json'
        //     ]
        // ]);
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/submit_audit?access_token='.$access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 小程序审核撤回
    public static function wxaUndocodeAudit($access_token)
    {
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/undocodeaudit?access_token='.$access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 新增临时素材
    public static function mediaUpload($component_appid,$appid,$images_path)
    {
        $accessToken = self::miniappAccessToken($component_appid,$appid);
        $content = file_get_contents($images_path);
        $images_name = md5(time().rand(10000,99999)).".jpg";
        $filePath = public_path("uploads/".$images_name);
        file_put_contents($filePath,$content);
        $client = new Client;
        $response = $client->request('POST','https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$accessToken["access_token"].'&type=image', [
            'multipart' => [
                [
                    'name'  => 'media',
                    'contents' => fopen($filePath, 'r'),
                ]
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        unlink($filePath);
        return $jsonbody;
    }

    // 查询最新一次提交的审核状态
    public static function getLatestAuditStatus($access_token)
    {
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token='.$access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 发布已通过审核的小程序
    public static function wxaRelease($access_token)
    {
        $client = new Client;
        $params = [];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/release?access_token='.$access_token, [
            'body' => '{}',
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取可回退的小程序版本
    public static function revertCodeRelease($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/revertcoderelease?access_token='.$authorizer_access_token.'&action=get_history_version');
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取小程序基本信息
    public static function getAccountBasicInfo($access_token)
    {
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo?access_token='.$access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取授权方的帐号基本信息
    public static function apiGetAuthorizerInfo($component_appid,$authorizer_appid)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $config = Config::get("wechats.open_platform.beianbao");
        $params = ["component_appid"=>$config["app_id"],"authorizer_appid"=>$authorizer_appid];
        $client = new Client;
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取可以设置的所有类目
    public static function getAllCategories($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/cgi-bin/wxopen/getallcategories?access_token='.$authorizer_access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 添加小程序类目
    public static function addCategory($access_token,$first,$second)
    {
        $client = new Client;
        $params = ["categories"=>["first"=>$first,"second"=>$second]];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/wxopen/addcategory?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 修改试用小程序名称
    public static function setBetaWeappNickname($component_appid,$authorizer_appid,$authorizer_refresh_token,$name)
    {
        $authorizer_access_token = WechatopenService::getMiniProgramAccessToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $params = ["name"=>$name];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/setbetaweappnickname?access_token='.$authorizer_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 修改小程序昵称
    public static function setNickname($access_token,$nick_name,$license,$naming_other_stuff)
    {
        $client = new Client;
        // $params = ["nick_name"=>$nick_name];
        $params = ["nick_name"=>$nick_name,"license"=>$license,"naming_other_stuff_1"=>$naming_other_stuff];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/setnickname?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 微信认证名称检测
    public static function checkWxVerifyNickname($access_token,$nick_name)
    {
        $client = new Client;
        $params = ["nick_name"=>$nick_name];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/wxverify/checkwxverifynickname?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 修改小程序头像
    public static function modifyHeadImage($access_token,$head_img_media_id)
    {
        $client = new Client;
        $params = ["head_img_media_id"=>$head_img_media_id,"x1"=>0,"y1"=>0,"x2"=>1,"y2"=>1];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/account/modifyheadimage?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 修改小程序简介
    public static function modifySignature($access_token,$signature)
    {
        $client = new Client;
        $params = ["signature"=>$signature];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/account/modifysignature?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 拉取授权方帐号基本信息
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/Account_Authorization/api_get_authorizer_list.html
    public static function apiGetAuthorizerList($component_appid)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $config = Config::get("wechats.open_platform.beianbao");
        $params = ["component_appid"=>$config["app_id"],"offset"=>0,"count"=>100];
        $client = new Client;
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 设置服务器域名
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Mini_Program_Basic_Info/Server_Address_Configuration.html
    public static function wxaModifyDomain($access_token,$action,$requestdomain,$wsrequestdomain,$uploaddomain,$downloaddomain,$udpdomain,$tcpdomain)
    {
        $client = new Client;
        if ($action == "get") {
            $params = ["action"=>$action];
        } else {
            $params = ["action"=>$action,"requestdomain"=>$requestdomain,"wsrequestdomain"=>$wsrequestdomain,"uploaddomain"=>$uploaddomain,"downloaddomain"=>$downloaddomain,"udpdomain"=>$udpdomain,"tcpdomain"=>$tcpdomain];
        }
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/modify_domain?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 设置业务域名
    public static function setWebviewDomain($access_token,$action,$webviewdomain)
    {
        $client = new Client;
        if ($action == "get") {
            $params = ["action"=>$action];
        } else {
            $params = ["action"=>$action,"webviewdomain"=>$webviewdomain];
        }
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/setwebviewdomain?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 跳转至第三方平台，第三方平台调用快速注册 API 完成管理员换绑
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/product/Register_Mini_Programs/fast_registration_of_mini_program.html
    public static function componentRebindAdmin($access_token,$taskid)
    {
        $client = new Client;
        $params = ["taskid"=>$taskid];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/account/componentrebindadmin?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 快速注册企业小程序
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/Fast_Registration_Interface_document.html
    public static function fastRegisterWeapp($component_appid,$name,$code,$code_type,$legal_persona_wechat,$legal_persona_name,$component_phone)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["name"=>$name,"code"=>$code,"code_type"=>$code_type,"legal_persona_wechat"=>$legal_persona_wechat,"legal_persona_name"=>$legal_persona_name,"component_phone"=>$component_phone];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create&component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }
    // 快速注册个人小程序
    public static function fastRegisterPersonalWeapp($component_appid,$idname,$wxuser,$component_phone)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["idname"=>$idname,"wxuser"=>$wxuser,"component_phone"=>$component_phone];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/fastregisterpersonalweapp?action=create&component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 快速注册企业小程序
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/Fast_Registration_Interface_document.html
    public static function fastRegisterWeappSearch($component_appid,$name,$legal_persona_wechat,$legal_persona_name)
    {
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["name"=>$name,"legal_persona_wechat"=>$legal_persona_wechat,"legal_persona_name"=>$legal_persona_name];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search&component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 查询搜索设置
    public static function getWxaSearchStatus($access_token)
    {
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/getwxasearchstatus?access_token='.$access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 修改搜索设置
    public static function changeWxaSearchStatus($access_token,$status)
    {
        $client = new Client;
        $params = ["status"=>$status];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/changewxasearchstatus?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 查询违规处罚记录
    public static function getIllegalRecords($access_token,$start_time,$end_time)
    {
        $client = new Client;
        $params = ["start_time"=>$start_time,"end_time"=>$end_time];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/getillegalrecords?access_token='.$access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取证件类型
    public static function queryIcpCertificateTypes($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizerAccessToken = self::apiAuthorizerToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/icp/query_icp_certificate_types?access_token='.$authorizerAccessToken);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取小程序服务内容类型
    public static function queryIcpServiceContentTypes($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizerAccessToken = self::apiAuthorizerToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/icp/query_icp_service_content_types?access_token='.$authorizerAccessToken);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取区域信息
    public static function queryIcpDistrictCode($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizerAccessToken = self::apiAuthorizerToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/icp/query_icp_district_code?access_token='.$authorizerAccessToken);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取单位性质
    public static function queryIcpSubjectTypes($component_appid,$authorizer_appid,$authorizer_refresh_token)
    {
        $authorizerAccessToken = self::apiAuthorizerToken($component_appid,$authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/wxa/icp/query_icp_subject_types?access_token='.$authorizerAccessToken);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 上传小程序备案媒体材料
    public static function uploadIcpMedia($appid,$certificate_type,$icp_order_field,$imageurl)
    {
        $content = file_get_contents($imageurl);
        $filePath = public_path("/images/miniapp/".$appid.".jpg");
        file_put_contents($filePath,$content);
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/icp/upload_icp_media?access_token='.$accessToken["access_token"], [
            'multipart' => [
                ['name'=>'type','contents'=>'image'],
                ['name'=>'certificate_type','contents'=>$certificate_type],
                ['name'=>'icp_order_field','contents'=>$icp_order_field],
                ['name'=>'media','contents'=>fopen($filePath,"r")]
            ]
        ]);

        // curl -F "type=image" -F "certificate_type=5" -F "icp_order_field=icp_applets.principal_info.certificate_photo_back" -F "media=@test.jpg" "https://api.weixin.qq.com/wxa/icp/upload_icp_media?access_token=ACCESS_TOKEN" 

        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 发起小程序管理员人脸核身
    public static function createIcpVerifyTask($appid)
    {
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        // $params = ["task_id"=>""];
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/icp/create_icp_verifytask?access_token='.$accessToken["access_token"], [
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 查询人脸核身任务状态
    public static function queryIcpVerifyTask($appid,$task_id)
    {
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        $params = ["task_id"=>$task_id];
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/icp/query_icp_verifytask?access_token='.$accessToken["access_token"], [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取小程序备案状态及驳回原因
    public static function getIcpEntranceInfo($component_appid,$appid)
    {
        $accessToken = self::miniappAccessToken($component_appid,$appid);
        $client = new Client;
        $params = [];
        $response = $client->request('GET','https://api.weixin.qq.com/wxa/icp/get_icp_entrance_info?access_token='.$accessToken["access_token"], [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 申请小程序备案
    public static function applyIcpFiling($appid,$params)
    {
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/icp/apply_icp_filing?access_token='.$accessToken["access_token"], [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 上传小程序认证媒体材料
    public static function uploadAuthMaterial($appid,$imageurl)
    {
        $content = file_get_contents($imageurl);
        $filePath = public_path("/images/miniapp/".$appid.time().".jpg");
        file_put_contents($filePath,$content);
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/sec/uploadauthmaterial?access_token='.$accessToken["access_token"], [
            'multipart' => [
                ['name'=>'type','contents'=>'image'],
                ['name'=>'media','contents'=>fopen($filePath,"r")]
            ]
        ]);

        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    //小程序提交/重新认证
    public static function wxaAuth($appid,$params)
    {
        $params = ["auth_data"=>$params];
        Log::info($params);
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/sec/wxaauth?access_token='.$accessToken["access_token"], [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    public static function reAuth($appid,$params)
    {
        $params = ["auth_data"=>$params];
        Log::info($params);
        $config = config("wechats.open_platform.beianbao");
        $accessToken = self::miniappAccessToken($config["app_id"],$appid);
        $client = new Client;
        $response = $client->request('POST','https://api.weixin.qq.com/wxa/sec/reauth?access_token='.$accessToken["access_token"], [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }
}